package com.company.project.common.job.task;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.company.project.common.config.SyncVoteUrlProperties;
import com.company.project.common.utils.UicodeBackslashU;
import com.company.project.entity.ProjectVoteEntity;
import com.company.project.service.ProjectVoteService;
import com.company.project.vo.resp.VoteData;

/**
 * 
 * Title: SyncVoteTask.java  
 * Description: 同步投票吧的投票数据
 * @author LS  
 * @date 2021-3-29 14:31:31  
 * @version 1.0
 */
@Component("syncVoteTask")
@EnableConfigurationProperties(SyncVoteUrlProperties.class)
public class SyncVoteTask {
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	SyncVoteUrlProperties syncVoteUrlProperties;
	
	@Autowired
    private ProjectVoteService projectVoteService;
	

	public void run(String params) {
		logger.info("SyncVoteTask定时任务正在执行");
		String url = syncVoteUrlProperties.getUrl();		
		Document doc;
		try {
			doc = Jsoup.connect(url).get();
			Elements scripts = doc.getElementsByTag("script").eq(3);
			for (Element ele : scripts) {
				// 爬取页面数据，截取投票数据部分
				String s = ele.data().toString();
				int b = s.indexOf("data:") + 5;
				int c = s.lastIndexOf("}]");
				String jsonData = s.substring(b, c);

				String newStr = UicodeBackslashU.unicodeToCn(jsonData);
				logger.info(newStr);
				List<VoteData> datas = JSON.parseArray(newStr, VoteData.class);
				// 按照票数排序,名次就为List下标+1
				List<VoteData> newDatas = datas.stream().sorted(Comparator.comparing(VoteData::getY).reversed())
						.collect(Collectors.toList());
				List<ProjectVoteEntity> pvs = new ArrayList<>();
				
				//List<MatchProjectEntity> projects = new ArrayList<>();
				for (int i = 0; i < newDatas.size(); i++) {
					String name = newDatas.get(i).getName();
					if (name != null) {
						int end = name.indexOf(".");
						int id = Integer.valueOf(name.substring(3, end));
						ProjectVoteEntity pv = projectVoteService.getById(id);
						logger.info("id="+id);
						if (pv == null) {
							logger.error("投票吧的序号"+id+"在project_vote表中不存在");
							continue ;
						}
						pv.setVote(newDatas.get(i).getY());
						pv.setRank(i + 1);						
						pvs.add(pv);
						
					}				
				}
				// project_vote表批量更新
				projectVoteService.updateBatchById(pvs);
				// 更新排名至match_project库
				//matchProjectService.updateBatchById(projects);
			}
		} catch (IOException e) {
			logger.error("同步投票数据失败");
			e.printStackTrace();
		}
	}

}
